package com.kk.xhr.admin.controller;

import com.kk.xhr.admin.biz.IDepartmentBizService;
import com.kk.xhr.admin.controller.request.DepartmentAddRequest;
import com.kk.xhr.admin.controller.request.DepartmentModifyRequest;
import com.kk.xhr.admin.controller.vo.DepartmentTreeVO;
import com.kk.xhr.admin.controller.vo.TeamVO;
import com.kk.xhr.common.pojo.BinaryEnum;
import com.kk.xhr.web.util.ShiroUtil;
import com.kk.xhr.web.vo.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

/**
 * 部门管理控制器
 *
 * @author kkmystery
 * @version 1.0 2021/3/5
 * @since 1.0.0
 */
@RestController
@RequiredArgsConstructor
@Api(value = "部门管理", tags = "部门管理")
@RequestMapping("/department")
public class DepartmentController {
    private final Executor adminControllerBizExecutor;
    private final IDepartmentBizService departmentBizService;

    @ApiOperation(value = "查询部门")
    @RequiresRoles(value = {"admin"})
    @GetMapping("/{id:.+}")
    public CompletionStage<ApiResult<DepartmentTreeVO>> query(@PathVariable("id") Long id) {
        return CompletableFuture.supplyAsync(() -> ApiResult.success(departmentBizService.query(id)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "查询我的部门成员")
    @GetMapping("/mine")
    public CompletionStage<ApiResult<TeamVO>> queryTeam() {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(departmentBizService.queryTeam(userId)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "查询是否为leader")
    @GetMapping("/leader")
    public CompletionStage<ApiResult<Boolean>> checkLeader() {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(departmentBizService.checkLeader(userId)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "添加部门")
    @RequiresRoles(value = {"admin"})
    @PostMapping
    public CompletionStage<ApiResult<Void>> add(@RequestBody @Valid DepartmentAddRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            departmentBizService.add(request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改部门")
    @RequiresRoles(value = {"admin"})
    @PutMapping("/{id:.+}")
    public CompletionStage<ApiResult<Void>> modify(@PathVariable("id") Long id,
                                                   @RequestBody @Valid DepartmentModifyRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            departmentBizService.modify(id, request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改部门状态")
    @RequiresRoles(value = {"admin"})
    @PutMapping("/status/{id:.+}")
    public CompletionStage<ApiResult<Void>> modifyStatus(@PathVariable("id") Long id, BinaryEnum status) {
        return CompletableFuture.supplyAsync(() -> {
            departmentBizService.modifyStatus(id, status);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }
}
